﻿define([
    'views/baseView',
    'models/crud/userSolutionModel',
    'text!templates/crud/crudBase.html',
    'text!templates/crud/crudForm.html',
    'text!templates/crud/solution/solutionCreate.html',
], function (BaseView, UserSolModel, BaseTmp, FormTmp, UniqTmp) {
    var view = BaseView.extend({

        $form: null,

        render: function () {
            toggleLoading(true);
            $("div[data-rel='crud_solution']").remove();

            var baseTemplate = Handlebars.compile(BaseTmp);
            //var formTemplate = Handlebars.compile(FormTmp);
            var uniqTemplate = Handlebars.compile(UniqTmp);

            var dataBaseOptions = {
                uuId: this._uId,
                dataRel: "crud_solution", // cuidado, ref en js
                tmpClass: "solution_new", // no esta en el html
                title: translate("_newSolution")
            };

            var html = baseTemplate(dataBaseOptions);
            //var html2 = formTemplate();
            var html3 = uniqTemplate();

            this.$el.append(html);
            this.$el.find(".crud_content").append(html3);

            this.$form = this.$el.find("form");

            postRender(this.$el);

            this.addHandlers();
            toggleLoading(false);
        },
        save: function () {
            if (this.$form.parsley('validate')) {
                var that = this;
                var name = this.$form.find("input[name='name']").val();
                var description = this.$form.find("input[name='description']").val();
                //var active = this.$form.find("input[name='active']").bootstrapSwitch('state');

                var userSolution = {
                    solution: {
                        name: name, description: description//, deleted: active
                    }
                };
                var model = new UserSolModel();
                model.create(userSolution, function (dataOk) {
                    $("#sidebar").trigger("newSolution", ["solutionName", "solutionId", "solutionExtra"]);
                    that.cancel();
                }, function (dataError) { });
            }
            /*
            if (!$(_cpyForm).validate().errorList.length > 0) {
                var _name = $("div[data-id='" + _uId + "']").find("div.company-name input").val();
                var _code = $("div[data-id='" + _uId + "']").find("div.company-code input").val();
                var _license = $("div[data-id='" + _uId + "']").find("div.company-license select").val();
                var _contact = $("div[data-id='" + _uId + "']").find("div.company-contact textarea").val();

                var company = {
                    code: _code, name: _name, contact: _contact, licenseType: _license
                };
                var model = new UserSolModel();
                model.Create(company, function () {
                    require(['controllers/abmManagers/companyManager'], function (Controller) {
                        (new Controller()).show();
                    });
                });
            }*/
        },
        // go back to solution manager
        cancel: function () {
            var that = this;
            require(['controllers/crud/solutionMngr'], function (Controller) {
                (new Controller()).show(that.$el);
            });
        },
        addHandlers: function () {
            this.$form.on("submit", $.proxy(function () {
                this.save();
                return false;
            }, this));
            //$("div[data-id='" + this._uId + "']").find("div.submit button.btn-cancel").on("click", $.proxy(function () {
            //    this.cancel();
            //    return false;
            //}, this));
            //$("div[data-id='" + this._uId + "']").find("div.abm-top-options a.btn-close").on("click", this.cancel);
            this.$form.find("button.btn-cancel").on("click", $.proxy(function () {
                this.cancel();
                return false;
            }, this));
        }
    });
    return view;
});